package com.example.plantplacessummer2013;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

import com.plantplaces.dto.PlantSearch;
import com.plantplaces.service.IPlantService;
import com.plantplaces.service.PlantService;

/**
 * Allow the user to search by plant criteria.
 * @author jonesbr
 *
 */
public class AdvancedPlantSearch extends PlantPlacesActivity {
		
	public static final String PLANT_SEARCH = "PLANT_SEARCH";
	
	private Button btnSearch;

	private AutoCompleteTextView actSearchName;

	private IPlantService plantService;

	private boolean resultRequired;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// this is where our activity starts.
		// first, the super keyword means we will invoke this same method on our superclasses.
		super.onCreate(savedInstanceState);
		// setContentView will set the layout that we are using.
		setContentView(R.layout.activity_advanced_plant_search);
				
		plantService = new PlantService(this);
		
		actSearchName = (AutoCompleteTextView) findViewById(R.id.actSearchName);
		
		// get the list of distinct genus. 
		try {
			// get the list of distinct genus.
			List<String> fetchGenus = plantService.fetchGenus();
			
			// create an array Adapter.
			ArrayAdapter genusAdapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, fetchGenus);
			
			// associate the array adapter with the autocomplete.
			actSearchName.setAdapter(genusAdapter);
			
			registerForContextMenu(actSearchName);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		btnSearch = (Button) findViewById(R.id.btnSearch);
		
		// make the button do something.
		OnClickListener ocl = new SearchButtonListener();
		btnSearch.setOnClickListener(ocl);
		
		resultRequired = getIntent().getBooleanExtra(RESULT, false);
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	/**
	 * Assemble a PlantSearch object.  Pass that object to the results page.
	 */
	private void search() {

		// get the text that the user entered.
		String searchText = actSearchName.getText().toString();
		
		if (!resultRequired) {
			
			// create and populate a plant search object.
			PlantSearch ps = new PlantSearch();
			ps.setGenus(searchText);
			
			// call an activity that will search and show results.
			Intent plantResultsIntent = new Intent(this, PlantResultsActivity.class);
	
			// pass along our search criteria to the resutls screen.
			plantResultsIntent.putExtra(PLANT_SEARCH, ps);
			
			// invoke the results screen
			startActivity(plantResultsIntent);
		} else {
			// return result to calling screen.
			Intent intent = this.getIntent();
			// the data we want to return to the calling screen.
			intent.putExtra(PLANT_RESULT, searchText);
			// tell the calling screen it went OK.
			this.setResult(RESULT_OK, intent);
			// close the current screen. Return to the calling screen.
			finish();
		}
	}

	class SearchButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			search();
		}
	}
	
	
}



